package com.situ.user.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.situ.user.bean.Shopcart;

public interface ShopcartMapper {
	@Delete({ "delete from shopcart", "where id = #{id,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer id);

	@Insert({ "insert into shopcart ( user_id, ", "goods_id, count)", "values ( #{userId,jdbcType=INTEGER}, ",
			"#{goodsId,jdbcType=INTEGER}, #{count,jdbcType=INTEGER})" })
	int insert(Shopcart record);

	@Select({ "select", "id, user_id, goods_id, count", "from shopcart", "where id = #{id,jdbcType=INTEGER}" })
	@ResultMap("shopcart")
	Shopcart selectByPrimaryKey(Integer id);

	@Select({ "select", "id, user_id, goods_id, count", "from shopcart" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
			@Result(column = "goods_id", property = "goodsId", jdbcType = JdbcType.INTEGER),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER) })
	List<Shopcart> selectAll();

	@Update({ "update shopcart", "set", "count = #{count,jdbcType=INTEGER}", "where id = #{id,jdbcType=INTEGER}" })
	int updateByPrimaryKey(Shopcart record);

	// 根据商品id和用户id查找购物车
	@Select("select * from shopcart where user_id = #{userId} and goods_id = #{goodsId}")
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
			@Result(column = "goods_id", property = "goodsId", jdbcType = JdbcType.INTEGER),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER) })
	Shopcart select(Shopcart cart);

	// 根据用户id查询购物车
	@Select("select * from shopcart where user_id = #{userId}")
	@Results(id = "shopcart", value = { @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
			@Result(column = "goods_id", property = "goodsId", jdbcType = JdbcType.INTEGER),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER),
			@Result(column = "goods_id", property = "goods", one = @One(select = "com.situ.user.mapper.GoodsMapper.selectByPrimaryKey")) })

	List<Shopcart> selectByUserId(Integer userId);
}